package cn.edu.ujn.shi.controller;

import cn.edu.ujn.shi.dao.Discuss;
import cn.edu.ujn.shi.dao.User;
import cn.edu.ujn.shi.service.IDiscussService;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletRequest;

import java.io.PrintWriter;
import java.util.List;

@Controller
public class DiscussController {

    @Autowired
    IDiscussService discussService;

    @RequestMapping("/ADeleteDiscussServlet/{id}")
    public void aDeleteDiscussServlet(@PathVariable("id") Integer id) {
        discussService.delete(id);
    }

    @RequestMapping("/AListDiscussServlet")
    public void aListDiscussServlet(HttpServletRequest request, HttpServletResponse response) {

        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            String username = request.getParameter("username");
            List<Discuss> list = discussService.getDiscussLike(username);

            PrintWriter out = response.getWriter();
            RequestDispatcher headerDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/header-admin.jsp");
            headerDispatcher.include(request, response);
            out.println("<center><img src='images/aa6.png' height=70px><font size='+4' face='楷体' color='#7D26CD'><b>评价管理</b></font>");
            out.println("<hr/>");

            out.println("<center>");
            out.println("    <form action='AListDiscussServlet' method='get'>\n" +
                    "        <label for='username'>按用户名搜索：</label>\n" +
                    "        <input type='text' id='username' name='username'>\n" +
                    "        <button type='submit'>搜索</button>\n" +
                    "    </form>");
            out.println("<br/><br/><table>");
            out.println("<tr>");

            out.println("<table width=90% border=1>");
            out.println("<tr bgcolor='#E0EEE0'>");
            out.println("<td>失物</td><td>锁定用户</td><td>承诺酬谢</td><td>发布账号</td><td>评价内容</td><td>操作</td>");
            out.println("</tr>");


            for (Discuss g : list) {
                out.println("<tr>");

                out.println("<td>");
                out.println(g.getName());
                out.println("</td>");
                out.println("<td>");
                out.println(g.getUsername());
                out.println("</td>");

                out.println("<td>");
                out.println(g.getMoney());
                out.println("</td>");
                out.println("<td>");
                out.println(g.getPeople());
                out.println("</td>");
                out.println("<td>");
                out.println(g.getDiscuss());
                out.println("</td>");

                out.println("<td>");
                out.println("<a href='ADeleteDiscussServlet?id=" + g.getId() + "'>删除评价</a>");
                out.println("</td>");
                out.println("</tr>");
            }

            out.println("</table>");

            out.println("<nav aria-label=\"Page navigation\">\n" +
                    "    <ul class=\"pagination\">\n" +
                    "        <a href=\"?page=1\">首页</a></li>\n" +
                    "        <a href=\"?page=1\">上一页</a></li>\n" +
                    "        1  <a href=\"?page=1>\">下一页</a></li>\n" +
                    "        <a href=\"?page=1>\">尾页</a></li>\n" +
                    "    </ul>\n" +
                    "</nav>");
//            out.println("<br/><br/><hr/><img src='images/bottom.png' width='100%'>");
//            out.println("</BODY>");
//
//            out.println("</HTML>");
            RequestDispatcher headerDispatcher1 = request.getRequestDispatcher("/WEB-INF/jsp/footer.jsp");
            headerDispatcher1.include(request, response);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/DeleteDiscussServlet")
    public void deleteDiscussServlet(HttpServletRequest request, HttpServletResponse response) {

        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");

            int id = Integer.parseInt(request.getParameter("id"));
            discussService.delete(id);
            response.sendRedirect("ListMyDiscussServlet");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/updateDiscuss")
    public void updateDiscuss(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            PrintWriter out = response.getWriter();

            HttpSession hs = request.getSession();
            String username = hs.getAttribute("username").toString();

            String id = request.getParameter("id");
            Discuss discuss = discussService.getDiscussById(id);

            //引用jsp文件
            RequestDispatcher headerDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/header.jsp");
            headerDispatcher.include(request, response);
            out.println("<center><hr/><img src='images/top6.jpg' width='100%'><hr/></center>");

            out.println("<br/>");
            out.println("<center><img src='images/u5.png' height=70px><font size='+4' face='楷体' color='#7D26CD'><b>评价信息</b></font>");
            out.println("<hr/>");

            out.println("<center>");
            out.println("<form action='UpdateDiscussServlet?name=" + discuss.getName() + "&&people=" + discuss.getPeople() + "&&money=" + discuss.getMoney() + "&id=" + discuss.getId() + "' method='post'>");
            out.println("<table>");
            out.println("<tr>");
            out.println("<td>");
            out.println("当前账号：");
            out.println("</td>");
            out.println("<td>");
            out.println(username);
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td>");
            out.println("失物信息：");
            out.println("</td>");
            out.println("<td>");
            out.println(discuss.getName());
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td>");
            out.println("发布账号：");
            out.println("</td>");
            out.println("<td>");
            out.println(discuss.getPeople());
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td>");
            out.println("承诺酬金：");
            out.println("</td>");
            out.println("<td>");
            out.println(discuss.getMoney());
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td>");
            out.println("我的评价:");
            out.println("</td>");
            out.println("<td>");
            out.println("<textarea name='discuss' style='height:200px;width:550px'>" + discuss.getDiscuss() + "</textarea>");
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td colspan='2'>");
            out.println("<input type='submit' value='评价' style='width:700px;'/>");
            out.println("</td>");
            out.println("</tr>");
            out.println("</table>");

            out.println("</form>");
            out.println("</center>");

            out.println("<br/><br/><hr/><img src='images/bottom.png' width='100%'>");
            out.println("</body>");
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("/InsertDiscussServlet")
    public void insertDiscussServlet(HttpServletRequest request, HttpServletResponse response) {

        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");

            HttpSession hs = request.getSession();
            String username = hs.getAttribute("username").toString();

            String name = request.getParameter("name");
            String money = request.getParameter("money");
            String people = request.getParameter("people");
            String discuss = request.getParameter("discuss");

            discussService.insert(new Discuss(name, username, money, discuss, people));
            response.sendRedirect("ListMyDiscussServlet");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/UpdateDiscussServlet")
    public void UpdateDiscussServlet(HttpServletRequest request, HttpServletResponse response) {

        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");

            HttpSession hs = request.getSession();
            String username = hs.getAttribute("username").toString();
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String money = request.getParameter("money");
            String people = request.getParameter("people");
            String discuss = request.getParameter("discuss");

            discussService.update(new Discuss(Integer.parseInt(id), name, username, money, discuss, people));
            response.sendRedirect("ListMyDiscussServlet");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/ListMyDiscussServlet")
    public void listMyDiscussServlet(HttpServletRequest request, HttpServletResponse response) {

        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");

            HttpSession hs = request.getSession();
            String username = hs.getAttribute("username").toString();

            List<Discuss> list = discussService.getDiscuss(username);

            PrintWriter out = response.getWriter();
            RequestDispatcher headerDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/header.jsp");
            headerDispatcher.include(request, response);

            out.println("<center><hr/><img src='images/top5.jpg' width='100%'><hr/></center>");

            out.println("<br/>");
            out.println("<center><img src='images/u5.png' height=70px><font size='+4' face='楷体' color='#7D26CD'><b>我的评价</b></font>");
            out.println("<hr/>");

            out.println("<center>");
            out.println("<br/><br/><table>");
            out.println("<tr>");

            out.println("<table width=90% border=1>");
            out.println("<tr bgcolor='#E0EEE0'>");
            out.println("<td align='center'>失物信息</td><td align='center'>锁定用户</td><td align='center'>承诺酬金</td><td align='center'>发布用户</td><td align='center'>评价内容</td><td align='center'>操作</td>");
            out.println("</tr>");


            for (Discuss g : list) {
                out.println("<tr>");

                out.println("<td align='center'>");
                out.println(g.getName());
                out.println("</td>");
                out.println("<td align='center'>");
                out.println(g.getUsername());
                out.println("</td>");

                out.println("<td align='center'>");
                out.println(g.getMoney());
                out.println("</td>");
                out.println("<td align='center'>");
                out.println(g.getPeople());
                out.println("</td>");
                out.println("<td align='center'>");
                out.println(g.getDiscuss());
                out.println("</td>");

                out.println("<td align='center'>");
                out.println("<a href='updateDiscuss?id=" + g.getId() + "'>更新评价</a>");
                out.println("<a href='DeleteDiscussServlet?id=" + g.getId() + "'>取消评价</a>");
                out.println("</td>");
                out.println("</tr>");
            }

            out.println("</table>");

//            out.println("<br/><br/><hr/><img src='images/bottom.png' width='100%'>");
//            out.println("</BODY>");
//
//            out.println("</HTML>");
            RequestDispatcher headerDispatcher1 = request.getRequestDispatcher("/WEB-INF/jsp/footer.jsp");
            headerDispatcher1.include(request, response);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


